package com.micai.springboot.service.impl;

import com.micai.springboot.entity.BookEntity;
import com.micai.springboot.repository.BookRepository;
import com.micai.springboot.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 描述：
 * <p>
 * Author: 赵新国
 * Date: 2018/1/23 16:32
 */
@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookRepository bookRepository;

    @Override
    public BookEntity save(BookEntity book) {
        BookEntity bookEntity = bookRepository.save(book);
        return bookEntity;
    }

    @Override
    public void delete(BookEntity book) {
        bookRepository.delete(book);
    }

    @Override
    public BookEntity findOne(String id) {
        BookEntity bookEntity = bookRepository.findOne(id);
        if (bookEntity == null) {
            return null;
        }
        return bookEntity;
    }

    @Override
    public Iterable<BookEntity> findAll() {
        Iterable<BookEntity> bookEntities = bookRepository.findAll();
        if (bookEntities == null) {
            return null;
        }
        return bookEntities;
    }

    @Override
    public Page<BookEntity> findByAuthor(String author, PageRequest pageRequest) {
        Page<BookEntity> bookEntityPage = bookRepository.findByAuthor(author, pageRequest);
        if (bookEntityPage == null) {
            return null;
        }
        return bookEntityPage;
    }

    @Override
    public List<BookEntity> findByTitle(String title) {
        List<BookEntity> bookEntities = bookRepository.findByTitle(title);
        if (bookEntities == null) {
            return null;
        }
        return bookEntities;
    }
}
